﻿using Microsoft.AspNetCore.Mvc.Filters;

namespace UI.Filters
{
    public class ValidateModelState : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (!context.ModelState.IsValid)
            {
                var error = context.ModelState.Where(m => m.Value.Errors.Count > 0)
                    .Select(e => e.Key + ": " + e.Value.Errors[0].ErrorMessage);
                throw new Exception($"Model验证失败。（{String.Join("，", error)}）" );
            }
            base.OnActionExecuting(context);
        }
    }
}
